package win.zxshuo.www.spider;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.stereotype.Component;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class BaseRequest {

    Log logger = LogFactory.getLog(getClass());

    private String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36";

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public List<String> getLocationsForUrlRedirect(String url,List<String> locations){
        try{
            URL requestUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
            conn.setRequestMethod("GET");
            conn.setInstanceFollowRedirects(false);
            conn.addRequestProperty("Accept-Charset","UTF-8");
            conn.addRequestProperty("User-Agent",getUserAgent());
            conn.addRequestProperty("Referer",url);
            conn.connect();
            String location = conn.getHeaderField("Location");
            if (location != null){
                locations.add(location);
            }
            String statusCode = conn.getHeaderField("Status Code");
            if (conn.getResponseCode() == 302 || conn.getResponseCode() == 301){
                getLocationsForUrlRedirect(location,locations);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return locations;
    }

    public String getDestUrl(String url){
        List<String> locations = new ArrayList<>();
        locations = getLocationsForUrlRedirect(url,locations);
        return locations.get(locations.size()-1);
    }


    public HttpEntity get(String url) throws Exception {
        HttpEntity entity ;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response = httpClient.execute(httpGet);
            try {
                entity = response.getEntity();
                String content = EntityUtils.toString(entity);
                Header[] headers = response.getAllHeaders();
                for (Header header:headers){
                    System.out.println(header);
                }
            }finally {
                response.close();
            }
        }finally {
            httpClient.close();
        }
        return entity;
    }



}
